package com.btkjgc.controller;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.TemplateHashModel;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

public class UtilDisplayController extends HashMap<Object, Object>
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -2713468351716531309L;
	private static UtilDisplayController FREEMARKER_STATIC_MODELS;
	private Properties staticModels;

	private UtilDisplayController()
	{

	}

	public static UtilDisplayController getInstance()
	{
		if (FREEMARKER_STATIC_MODELS == null)
		{
			FREEMARKER_STATIC_MODELS = new UtilDisplayController();
		}
		return FREEMARKER_STATIC_MODELS;
	}

	public Properties getStaticModels()
	{
		return staticModels;
	}

	public void setStaticModels(Properties staticModels)
	{
		if (this.staticModels == null && staticModels != null)
		{
			this.staticModels = staticModels;
			Set<String> keys = this.staticModels.stringPropertyNames();
			for (String key : keys)
			{
				FREEMARKER_STATIC_MODELS.put(key,
						useStaticPackage(this.staticModels.getProperty(key)));
			}
		}
	}

	public static TemplateHashModel useStaticPackage(String packageName)
	{
		try
		{
			BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
			TemplateHashModel staticModels = wrapper.getStaticModels();
			TemplateHashModel fileStatics = (TemplateHashModel) staticModels
					.get(packageName);
			return fileStatics;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
}